package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.AddCartsForm;
import com.cskaoyan.gateway.form.shopping.CheckAllCartItemForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liXiaoFei
 * @date 2021/10/31 11:35
 */
@RestController
@RequestMapping("shopping")
public class CartsController {

    @Reference(timeout = 3000,retries = 0, check = false)
    ICartService iCartService;

    /**
     * 获取当前用户的购物车
     * @param request HttpServletRequest → 用于获取用户的信息
     * @return 购物车结果
     */
    @GetMapping("carts")
    public ResponseData getCartsList(HttpServletRequest request){
        Long userId = getUserId(request);
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userId);
        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 添加商品至购物车
     * @param addCartsForm 用户id、商品id、商品数量
     * @return 添加结果
     */
    @PostMapping("carts")
    public ResponseData addCarts(@RequestBody AddCartsForm addCartsForm){
        AddCartRequest request = new AddCartRequest();
        request.setUserId(addCartsForm.getUserId());
        request.setItemId(addCartsForm.getProductId());
        request.setNum(addCartsForm.getProductNum());
        AddCartResponse response = iCartService.addToCart(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 修改购物车中商品的数量
     * @param addCartsForm 用户id、商品id、商品数量
     *                     和添加商品用的一致
     * @return 修改结果
     */
    @PutMapping("carts")
    public ResponseData putCarts(@RequestBody AddCartsForm addCartsForm){
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(addCartsForm.getUserId());
        request.setItemId(addCartsForm.getProductId());
        request.setNum(addCartsForm.getProductNum());
        request.setChecked(addCartsForm.getChecked());
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 删除购物车中的商品
     * @param uid 用户id
     * @param pid 要删除的商品id
     * @return 删除结果
     */
    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCartsGoodByGoodsId(@PathVariable("uid")Long uid,
                                                 @PathVariable("pid")Long pid){
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 删除购物车中选中的商品
     * @param uid 用户id
     * @return 删除结果
     */
    @DeleteMapping("items/{uid}")
    public ResponseData deleteCartsSelectedItems(@PathVariable("uid")Long uid){
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);
        DeleteCheckedItemResposne resposne = iCartService.deleteCheckedItem(request);
        if (resposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(resposne.getMsg());
        }
        return new ResponseUtil().setErrorMsg(resposne.getMsg());
    }

    /**
     * 全选购物车中的商品
     * @param checkAllCartItemForm 用户id、是否全选
     * @return 返回结果
     */
    @PutMapping("items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllCartItemForm checkAllCartItemForm){
        CheckAllItemRequest request = new CheckAllItemRequest();
        request.setUserId(checkAllCartItemForm.getUserId());
        request.setChecked(checkAllCartItemForm.getChecked());
        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 清空当前用户购物车
     * @param checkAllCartItemForm 用户id、购物车中商品列表
     * @return 清空结果
     */
    @DeleteMapping("I don't know url")
    public ResponseData clearCartItemByUserID(@RequestBody CheckAllCartItemForm checkAllCartItemForm){

        return null;
    }


    /**
     * 获取当前用户的id
     * @param request HttpServletRequest
     * @return 用户id
     */
    private Long getUserId(HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        return Long.parseLong(object.get("uid").toString());
    }

}
